home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GSCHAR0.C < prev    next >
C/C++ Source or Header  |  1991-01-29  |  5KB  |  178 lines

  1. /* Copyright (C) 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gschar0.c */
  21. /* Composite font decoding for Ghostscript library */
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"            /* for gzstate.h */
  26. #include "gzstate.h"            /* must precede gzdevice */
  27. #include "gzdevice.h"            /* must precede gxchar */
  28. #include "gxdevmem.h"
  29. #include "gxchar.h"
  30. #include "gxfont.h"
  31.  
  32. /* Get the next character and font from a composite string. */
  33. /* If we run off the end of the string in the middle of a */
  34. /* multi-byte sequence, return gs_error_rangecheck. */
  35. /* If the string is empty, return 1.  Otherwise, return 0. */
  36. int
  37. gs_type0_next(gs_show_enum *penum, ulong *pchar)
  38. {    uint index = penum->index;
  39.     byte *p = penum->str + index;
  40.     uint size = penum->size;
  41.     uint left;
  42.     gs_font *pfont = penum->fstack[penum->fdepth];
  43.     gs_type0_data *pdata = &pfont->data.type0_data;
  44.     uint fidx;
  45.     int modal_mapping;
  46. top:    if ( (left = size - index) == 0 ) return 1;
  47. #define need_left(n)\
  48.   if ( left < n ) return_error(gs_error_rangecheck)
  49.     switch ( pdata->FMapType )
  50.        {
  51.     default:            /* can't happen */
  52.         return_error(gs_error_invalidfont);
  53.  
  54.         /* ------ Non-modal mappings ------ */
  55.  
  56.     case 2:                /* 8/8 mapping */
  57.         modal_mapping = 0;
  58.         need_left(2);
  59.         fidx = *p++;
  60.         *pchar = *p++;
  61.         break;
  62.  
  63.     case 4:                /* 1/7 mapping */
  64.         modal_mapping = 0;
  65.         fidx = *p >> 7;
  66.         *pchar = *p++ & 0x7f;
  67.         break;
  68.  
  69.     case 5:                /* 9/7 mapping */
  70.         modal_mapping = 0;
  71.         need_left(2);
  72.         fidx = ((uint)*p << 1) + (p[1] >> 7);
  73.         *pchar = p[1] & 0x7f;
  74.         p += 2;
  75.         break;
  76.  
  77.     case 6:                /* SubsVector mapping */
  78.         modal_mapping = 0;
  79.        {    uint subs_count = pdata->subs_size;
  80.         byte *psv = pdata->SubsVector;
  81.         need_left(pdata->subs_width);
  82. #define subs_loop(subs_elt, width)\
  83.   while ( subs_count != 0 && chr >= (schr = subs_elt) )\
  84.     subs_count--, chr -= schr, psv += width;\
  85.   *pchar = chr; break
  86.         switch ( pdata->subs_width )
  87.            {
  88.         default:        /* can't happen */
  89.             return_error(gs_error_invalidfont);
  90.         case 1:
  91.            {    byte chr = *p, schr;
  92.             subs_loop(*psv, 1);
  93.            }
  94.         case 2:
  95. #define w2(p) (((ushort)*p << 8) + p[1])
  96.            {    ushort chr = w2(p), schr;
  97.             subs_loop(w2(psv), 2);
  98.            }
  99. #undef w2
  100.         case 3:
  101. #define w3(p) (((ulong)*p << 16) + ((uint)p[1] << 8) + p[2])
  102.            {    ulong chr = w3(p), schr;
  103.             subs_loop(w3(psv), 3);
  104.            }
  105. #undef w3
  106.         case 4:
  107. #define w4(p)\
  108.   (((ulong)*p << 24) + ((ulong)p[1] << 16) + ((uint)p[1] << 8) + p[2])
  109.            {    ulong chr = w4(p), schr;
  110.             subs_loop(w4(psv), 4);
  111.            }
  112. #undef w4
  113. #undef subs_loop
  114.            }
  115.         p += pdata->subs_width;
  116.         break;
  117.        }
  118.  
  119.         /* ------ Modal mappings ------ */
  120.  
  121.     case 3:                /* escape mapping */
  122.         modal_mapping = 1;
  123.         if ( *p == pdata->EscChar )
  124.            {    need_left(2);
  125.             fidx = p[1]; p += 2; goto remap;
  126.            }
  127.         goto modal;
  128.  
  129.     case 7:                /* double escape mapping */
  130.         modal_mapping = 1;
  131.         if ( *p == pdata->EscChar )
  132.            {    need_left(2);
  133.             if ( p[1] == pdata->EscChar )
  134.                {    need_left(3);
  135.                 fidx = p[2] + 256; p += 3; goto remap;
  136.                }
  137.             fidx = p[1]; p += 2; goto remap;
  138.            }
  139.         goto modal;
  140.  
  141.     case 8:                /* shift mapping */
  142.         modal_mapping = 1;
  143.         if ( *p == pdata->ShiftIn )
  144.            {    fidx = 0; p++; goto remap;
  145.            }
  146.         else if ( *p == pdata->ShiftOut )
  147.            {    fidx = 1; p++; goto remap;
  148.            }
  149. modal:        *pchar = *p++;
  150.         penum->index = p - penum->str;
  151.         return 0;
  152.  
  153.        }
  154.  
  155.     /* Control continues here for non-modal mappings, */
  156.     /* or for modal mappings after a font change. */
  157. remap:    if ( fidx >= pdata->encoding_size )
  158.         return_error(gs_error_rangecheck);
  159.     fidx = pdata->Encoding[fidx];
  160.     penum->index = p - penum->str;
  161.     /* We pre-checked the encoding vector, so we know that */
  162.     /* fidx is now a legal subscript for FDepVector. */
  163.     pfont = pdata->FDepVector[fidx];
  164.     if ( modal_mapping )
  165.        {    if ( penum->index == 0 ) return 1;
  166.         if ( pfont->FontType == ft_composite )
  167.            {    penum->fstack[++penum->fdepth] = pfont;
  168.             goto top;
  169.            }
  170.         *pchar = *p++;
  171.         penum->index--;
  172.        }
  173.     else
  174.        {    penum->pfont = pfont;
  175.        }
  176.     return 0;
  177. }
  178.